package com.xy.work.template.app.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xy.work.template.app.domain.User;
import com.xy.work.template.app.domain.vo.UserVo;
import com.xy.work.template.app.mapper.UserMapper;
import com.xy.work.template.app.service.IUserService;
import com.xy.work.template.base.service.impl.BaseServiceImpl;
import com.xy.work.template.base.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2021-08-16
 */
@Service
public class IUserServiceImpl extends  BaseServiceImpl<User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User getPhoneUser(String phone) {
        if(StrUtil.isEmpty(phone)){
            return null;
        }
        QueryWrapper<User> query =new QueryWrapper<>();
        query.eq("phone",phone);
        return userMapper.selectOne(query);
    }
    @Override
    public Map<String, Object> getPageList(Integer page, Integer size) {
        QueryWrapper<User> query =new QueryWrapper<>();
//        query.eq("sex", 1);
        Page<User> pageInfo = new Page<>(page,size);
        IPage<User> list=userMapper.selectPage(pageInfo, query);
        Map<String,Object> pageMap = new HashMap<>(3);
        //总条数
        pageMap.put("total_record", list.getTotal());
        //总页数
        pageMap.put("total_page",list.getPages());
        //将数据转换成向外展示的实体类
        pageMap.put("current_data",list.getRecords().stream().map(this::beanProcess).collect(Collectors.toList()));
        return pageMap;
    }

    @Override
    public int addUser(User data) {
        return userMapper.insert(data);
    }

    /**
     * 复制信息
     * @param obj
     * @return
     */
    private UserVo  beanProcess(User obj) {
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(obj,userVo);
        return userVo;
    }
}
